#!/usr/bin/env python
import time
import XenAPI
import parse_rrd

def get_xapi_session(url):
    session = XenAPI.Session(url)
    session.xenapi.login_with_password('root','5531248yu')
    return session

def print_latest_host_data(rrd_updates):
    host_uuid = rrd_updates.get_host_uuid()
    print "**********************************************************"
    print "Got values for Host: "+host_uuid
    print "**********************************************************"

    for param in rrd_updates.get_host_param_list():
        if param != "":
            max_time=0
            data=""
            for row in range(rrd_updates.get_nrows()):
                epoch = rrd_updates.get_row_time(row)
                dv = str(rrd_updates.get_host_data(param,row))
                if epoch > max_time:
                    max_time = epoch
                    data = dv
            nt = time.strftime("%H:%M:%S", time.localtime(max_time))
            print "%-30s             (%s , %s)" % (param, nt, data)


def get_latest_vm_data(rrd_updates,uuid):
    param_list=[]
    data_list=[]
    for param in rrd_updates.get_vm_param_list(uuid):
        if param != "":
            max_time=0
            data=""
            for row in range(rrd_updates.get_nrows()):
                epoch = rrd_updates.get_row_time(row)
                dv = str(rrd_updates.get_vm_data(uuid,param,row))
                if epoch > max_time:
                    param_list.append(param)
                    max_time = epoch
                    data = dv
                    data_list.append(data)
                    vm_param_data=dict(zip(param_list,data_list))
    return vm_param_data

def build_vm_graph_data(rrd_updates, vm_uuid, param):
    time_now = int(time.time())
    for param_name in rrd_updates.get_vm_param_list(vm_uuid):
        if param_name == param:
            data = "#%s  Seconds Ago" % param
            for row in range(rrd_updates.get_nrows()):                
                epoch = rrd_updates.get_row_time(row)
                data = str(rrd_updates.get_vm_data(vm_uuid, param_name, row))
                data += "\n%-14s %s" % (data, time_now - epoch)
            return data

def get_vm_param_values(url,uuid):
    session=get_xapi_session(url)
    vms = session.xenapi.VM.get_all()
    rrd_updates = parse_rrd.RRDUpdates()                                                                                                                              
    params = {}
    params['cf'] = "AVERAGE"
    params['start'] = int(time.time()) - 10
    params['interval'] = 5
    params['host'] = ""
    rrd_updates.refresh(session.handle, params, url)
    vm_param_value=get_latest_vm_data(rrd_updates,uuid)
    return vm_param_value

def get_vm_list():
    url = "https://10.20.0.1:443"
    session=get_xapi_session(url)
    vms = session.xenapi.VM.get_all()
    vm_list=[]
    vm_param=['name','uuid','value']
    for vm in vms:
        record = session.xenapi.VM.get_record(vm)
        if not(record["is_a_template"]) and not(record["is_control_domain"] and record["power_state"] == "Running"):
            name = record["name_label"]
            print "vm_name:"+name
            uuid= record["uuid"]
            vm_params_value=get_vm_param_values(url,uuid)
            data_list_value=[]
            data_list_value.append(name)
            data_list_value.append(uuid)
            data_list_value.append(vm_params_value)
            vm_value=dict(zip(vm_param,data_list_value))
            vm_list.append(vm_value)
    return vm_list
    


                    #param = 'cpu0'
                    #data = build_vm_graph_data(rrd_updates, uuid, param)
                    #fh = open("%s-%s.dat" % (uuid, param), 'w')
                    #fh.write(data)
                    #fh.close()

def main():
    get_vm_list()
main()

    